Decision Making in C
Decision making is the process of deciding whether a block of code can be executed or not based on certain condition.
If the condition is satisfied, then the block of statement will be executed, else the block of statement will not be executed.
In C, there are 3 types of decision making statements. They are
if
statementif-else
statementelse-if
statementswitch
statement
if
statement
The if
statement is used to check for any condition(s) and if the condition is true then it executes the block of statement after it.
Syntax
if(condition)
{
// block of statements.
}
Example
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 10;
if(x>y)
{
printf("%d is greater than %d",x,y);
}
getch();
}
Output
15 is greater than 10
if-else
statement
The if-else
statement is used to check for a condition, if the condition is true then the if block will be executed. If the condition is false then, the else statement will be executed.
Syntax
if(condition)
{
// block of statements
}
else
{
// block of statements
}
Example
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 20;
if(x>y)
{
printf("%d is greater than %d",x,y);
}
else
{
printf("%d is greater than %d",y,x);
}
getch();
}
Output
20 is greater than 15
else-if
statement
The else-if
statement is used to check more than two conditions.
Syntax
if(condition-1)
{
// block of statement.
}
else if(condition-2)
{
// block of statement.
}
else if(condition-3)
{
// block of statement.
}
else
{
// block of statement.
}
Here the conditions are executed from the top order. If any condition is satisfied down the order, then that statement will be executed, if none of the condition is satisfied, then the else
block will be executed.
Example
#include <stdio.h>
void main( )
{
int x, y,z;
printf("Enter 3 numbers to find the biggest amoung them\n");
scanf("%d%d%d",&x,&y,&z);
if(x>y && x>z)
{
printf("%d is greater than %d and %d",x,y,z);
}
else if(y>z)
{
printf("%d is greater than %d and %d",y,x,z);
}
else{
printf("%d is greater than %d and %d",z,x,y);
}
getch();
}
Nested if-else
statement
There may be some scenario in which we need to check some condition inside the if
statement. We can have multiple if
statement inside an if
or else
statement.
Syntax
if(condition)
{
// block of statement.
if(condition)
{
// block of statement.
}
}
else
{
// block of statement.
if(condition)
{
// block of statement.
}
}
Example
#include <stdio.h>
void main( )
{
int x, y,z;
printf("Enter 3 numbers to find the biggest amoung them\n");
scanf("%d%d%d",&x,&y,&z);
if(x>y)
{
if(x>z)
{
printf("%d is greater than %d and %d",x,y,z);
}
}
else if(y>z)
{
printf("%d is greater than %d and %d",y,x,z);
}
else{
printf("%d is greater than %d and %d",z,x,y);
}
getch();
}
Switch Statement
If we need to check a list of conditions and execute the set of statements based on condition we can go for else-if
statement as well as switch
statement.
The expression is given in the switch
and check is done in the case
, hence its called switch-case
.
Syntax
switch(expression/variable)
{
case value:
//statements
break; //optional
default: //optional
//statements
break;
}
Example
#include<stdio.h>
#include<conio.h>
void main() {
int day;
clrscr();
printf("Enter any number from 1 to 7\n");
scanf("%d", & day);
switch (day) {
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Invalid option provided.");
}
getch();
}
Output 1
Enter any number from 1 to 7
1
Sunday
Output 2
Enter any number from 1 to 7
8
Invalid option provided.
In the above example, we ask for an integer value from 1 to 7 and store it in variable day.
Now we pass the day variable to switch
statement. The case
statement is used to check whether the value of day variable(defined in switch statement) is from 1 to 7. If the condition is true, then it will execute the block inside that case
statement.
When a break
statement is reached, the switch
is terminated and the flow of the program moves to the next line following the switch
statement.
If, no case
statements are satisfied, then statement inside the default
will be executed.
Rules
- With switch statement can use only byte, short, int, char data type.
- The value for a case must be same as the variable in switch.
- If no break appears, then the flow of the program will fall through to subsequent cases until a break is reached.